const { ccclass, property } = cc._decorator;
import loadGame from '../Load/loadGame';
import Background from './Background';
import WorldNodeControl from '../World/WorldNodeControl';
import MonstersBilderControl from '../Monster/MonstersBilderControl';
import SetCollisionPhysics from './SetCollisionPhysics';
import gameControl from './gameControl';
import AttackNodeControl from '../SkillAttack/AttackNodeControl';
import SkillBuild from '../SkillBuild/SkillBuild';
import RandomPropCOntrol from '../RandomProp/RandomPropControl';
import loadLobby from '../Load/loadLobby';

@ccclass
export default class PlayerInit extends cc.Component {
    @property(cc.Node)
    objectNode: cc.Node = null;
    @property(cc.Node)
    SkillStore: cc.Node = null;
    @property(cc.Node)
    SceneryNode: cc.Node = null;
    @property(cc.Node)
    WorldNode: cc.Node = null;
    @property(cc.Node)
    MonsterNode: cc.Node = null;
    @property(cc.Node)
    AttackNode: cc.Node = null;
    @property(cc.Node)
    SkillsNode: cc.Node = null;
    @property(cc.Node)
    RandomPropNode: cc.Node = null;
    _loadGame: loadGame = null;
    onLoad() {
        let gameJson = gameHelper.getInstance().getLevelParam();
        //获取常驻节点所绑定的脚本 
        this._loadGame = cc.director.getScene().getChildByName('loadGame').getComponent(loadGame);

        //获取常驻节点所绑定的脚本 
        let lobbyNode = cc.director.getScene().getChildByName('loadLobby').getComponent(loadLobby);

        let objectControl = this.objectNode.getComponent(SetCollisionPhysics);
        objectControl.heroPfb = this._loadGame.heroPrefab
        this.SkillStore.getChildByName("speedUp").getComponent(cc.Sprite).spriteFrame = this._loadGame.speedUpAtlas.getSpriteFrame("hero");
        let _gameControl = this.node.getComponent(gameControl);
        _gameControl.ScoreAward = gameJson.ScoreAward;
        _gameControl.FirstAward = gameJson.FirstAward;
        _gameControl.RandomAward = gameJson.RandomAward;
        _gameControl.pass = gameJson.Pass;
        let background = this.SceneryNode.getComponent(Background);
        background.fly = this._loadGame.SceneryFly;
        background.bg = this._loadGame.SceneryBg;
        let Attack = this.AttackNode.getComponent(AttackNodeControl);
        Attack.statusPrefabs = this._loadGame.statusPrefabs;
        Attack.refreshPrefabs = this._loadGame.refreshPrefabs;
        let skill = this.AttackNode.getComponent(SkillBuild);
        skill._SkillGunPrefab = this._loadGame.SkillGunPrefab;
        skill._icons = lobbyNode.SkillIcons;
        skill._skillBtBg = this._loadGame.SkillBtBg;
        skill._ParticlePrefab = this._loadGame.ParticlePrefab;
        skill._heroSkills = lobbyNode.SkillsJson.SkillHs;
        skill._defaultSkill = lobbyNode.SkillsJson.DefaultSkill;
        skill._monsterSkills = this._loadGame.MonsterJson.SkillMs;
        skill._strong = this._loadGame.Skill_strong;
        skill._itemBtPrefab = this._loadGame.SkillBtPrefab;
        skill.init();
        let popskill = this.RandomPropNode.getComponent(RandomPropCOntrol);
        popskill._RandomJson = this._loadGame.RandomJson;
        popskill._hitSkills = gameJson.HitSkills;
        popskill._selfSkills = gameJson.SelfSkills;
        popskill._ParticlePrefab = this._loadGame.ParticlePrefab;
        popskill._itemBtPrefab = this._loadGame.SkillBtPrefab;
        popskill._pickupPrefab = this._loadGame.pickupPrefab;
        popskill._icons = lobbyNode.SkillIcons;
        popskill._elevate = this._loadGame.Skill_elevate;
        popskill._skillBtBg = this._loadGame.SkillBtBg;
        popskill.init();
        let world = this.WorldNode.getComponent(WorldNodeControl);
        world._LevelsConfig = this._loadGame.worldParam;
        world.configId = gameJson.World;
        world._sprites = this._loadGame.WorldAtlas.getSpriteFrames();
        world.runWayPrefab = this._loadGame.runWayPrefab;
        world.wallWayPrefab = this._loadGame.wallWayPrefab;
        world.propsPrefab = this._loadGame.propsPrefab;
        world.flyWayPrefab = this._loadGame.flyWayPrefab;
        world.placeWayPrefab = this._loadGame.placeWayPrefab;

        let monster = this.MonsterNode.getComponent(MonstersBilderControl);
        monster.MonsterBilder = gameJson.Monster;
        monster.Blasts = this._loadGame.MonsterJson.Blasts;
        monster.Strike = this._loadGame.MonsterJson.Strike;
        monster.MonsterPrefab = this._loadGame.MonsterPrefab;
        monster.MonsterAtlas = this._loadGame.MonsterAtlas;

        this.initWorld();
        this.setZindex();
    }
    initWorld(): any {
        HeroHelper.getInstance().widthScene = this._loadGame.worldParam.Width;
        this.SceneryNode.getComponent(Background).init();
    }
    setZindex() {
        this.MonsterNode.zIndex = this.objectNode.zIndex + 1;
        this.SkillsNode.zIndex = this.MonsterNode.zIndex + 1;
    }
}
